﻿using System.Windows;
using System.Windows.Controls;
using System.ServiceModel.DomainServices.Client;
using System;
using WebSilverlight.Web;
using WebSilverlight.Helpers;
using WebSilverlight.Controls;

namespace WebSilverlight.Views
{
	public partial class PaisEditForm : StackPanel
	{
	#region Properties
				
		private PaisEditWindow _parentWindow;
		public PaisEditWindow ParentWindow
		{ 
			get
			{
				return this._parentWindow;
			}
			set
			{
				this._parentWindow = value;
				this.Current = this._parentWindow.Current;  
			} 
		}
		public bool DialogResult { get; set; }
		public bool HideActions 
        {
            set
            {
                if(value)
                    this.BottomActions.Visibility = System.Windows.Visibility.Collapsed;
                else
                    this.BottomActions.Visibility = System.Windows.Visibility.Visible;
            }
        }
		public bool IsReadOnly{ 
			get
			{
				return this.dataFormPais1.IsReadOnly;
			}
			set
			{
				this.EditButton.Visibility = System.Windows.Visibility.Collapsed;
				if(value)                
					this.EditButton.Visibility = System.Windows.Visibility.Visible;
				this.dataFormPais1.IsReadOnly = value;
			}
		}
		private PaisWrapper _Current;
		public PaisWrapper Current
		{
			set {
				this._Current = value;
				this.dataFormPais1.CurrentItem = value;
			}
			get 
			{ 
				return this._Current; 
			}
		}
	#endregion

		public PaisEditForm()
		{
			InitializeComponent();
			this.DialogResult = false;
			this.IsReadOnly = true;
		}
		/// <summary>        
        /// Data Validation
        /// </summary>  
        public bool Validate()
        {
            bool formValidation = dataFormPais1.ValidateItem();
            bool customValidation = CustomValidate();

            return formValidation && customValidation;
        }
        /// <summary>        
        /// Custom Data Validation
        /// </summary>  
        public bool CustomValidate()
        {            
			//TODO: Blob and other non-annotated validations...
            return true;            
        }        
		/// <summary>
		/// Ok Button Clicked
		/// TODO: Hacer l�gica de Navegaci�n
		/// </summary>  
		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			if (Validate())
			{                
				this.DialogResult = true;                           
				this.dataFormPais1.CommitEdit(true);                                
								
				if (IsReadOnly) 
					NavigateOK();					
				else
				{
					if (this.ParentWindow.IsNew)
					{   
						this.ParentWindow.DomainContext.PaisWrappers.Add(Current);						
						this.ParentWindow.DomainContext.SubmitChanges(Submit_Completed, null);                        												
					}
					else if (this.Current.HasChanges)
                    {						
                        this.ParentWindow.DDS.SubmitChanges();
                        this.ParentWindow.DDS.SubmittedChanges += new System.EventHandler<SubmittedChangesEventArgs>(DDS_SubmittedChanges);                     
                    }
                    else
                    {
                        NavigateOK();
                    }
				}
			}
		}
		/// <summary>
		/// Submit CallBack		
		/// </summary>  
		private void Submit_Completed(SubmitOperation invOp)
        {
            if (invOp.HasError)
            {
				this.dataFormPais1.ValidationSummary.Errors.Add(new ValidationSummaryItem(invOp.Error.Message));                                
                invOp.MarkErrorAsHandled();
            }
            else
            {
                NavigateOK();             
            }
        }
        /// <summary>
		/// DDS Submit CallBack		
		/// </summary> 
        void DDS_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            this.ParentWindow.DDS.SubmittedChanges -= this.DDS_SubmittedChanges;
            if (e.HasError)
            {
                this.dataFormPais1.ValidationSummary.Errors.Add(new ValidationSummaryItem(e.Error.Message));                
                e.MarkErrorAsHandled();
            }
            else
            {
                NavigateOK();
            } 
            
        }
		/// <summary>
		/// Cancel Button Clicked
		/// </summary>   
		private void CancelButton_Click(object sender, RoutedEventArgs e)
		{
			this.DialogResult = false;
			this.dataFormPais1.CancelEdit();			
			
			NavigateCancel();
		}
		/// <summary>
		/// Edit Button Clicked
		/// </summary>   
		private void EditButton_Click(object sender, RoutedEventArgs e)
		{
			this.ParentWindow.NavigateToEdit();
		}
		
		/// <summary>
		/// Navigate on OK
		/// </summary>  
		private void NavigateOK()
        {
            if (this.ParentWindow.IsNew)
                this.ParentWindow.IsNew = false;
            if (this.IsReadOnly)
                this.ParentWindow.NavigateToEnd();
            else
            {
                if (this.ParentWindow.IsEditingFirstState)
                    this.ParentWindow.NavigateToEnd();
                else
                    this.ParentWindow.NavigateToView();
            }
        }
        /// <summary>
		/// Navigate on Cancel
		/// </summary> 
        private void NavigateCancel()
        {
            if (this.IsReadOnly) 
                this.ParentWindow.NavigateToEnd();
            else if (this.ParentWindow.IsEditingFirstState) 
                this.ParentWindow.NavigateToEnd();
            else 
                this.ParentWindow.NavigateToView();
        }
		
		 									
	}
}